﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ValuationBLL;
using ValuationModel;

namespace ValuationServers.Controllers
{
    public class ValuesController : ApiController
    {
        Validte vld = new Validte();
        UsersBll bll = new UsersBll();
        #region 登录
        //验证码
        [HttpGet]
        public dynamic GetCodeImg(string t)
        {
            string code = vld.CreateRandomCode(4);
            byte[] imgByte = vld.CreateValidateGraphic(code);
            return new
            {
                code = code.ToLower(),
                imgByte = imgByte
            };
        }
        //登录方法
        public dynamic Get([FromUri]string name, string pwd, string yzm, string LoginCode)
        {
            var user = bll.Login<SysUser>(name, pwd);
            if (yzm.ToString().ToUpper() == LoginCode.ToUpper().ToString())
            //判断验证码是否正确
            {
                if (user != null && user.users_Id != 0)
                {

                    return user;  //返回一个对象
                }
                else
                {
                    return null;//返回一个null
                }
            }
            return 1;//

        }
        #endregion
        #region 左侧导航栏
        /// <summary>
        /// 调用权限关系的父级
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        [HttpGet]
        public string GetAuthorityRole(int RoleId)
        {
            var AuthorityRoleList = bll.Authority().Where(t => t.roles_Id == RoleId && t.ParentId == 0).ToList();
            return JsonConvert.SerializeObject(AuthorityRoleList);
        }
        /// <summary>
        /// 调用权限关系的子级
        /// </summary>
        /// <param name="RoleId"></param>
        /// <param name="ArId"></param>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<SysAuthorityAndRoles> GetParentId(int RoleId)
        {
            var ParentList = bll.Authority().Where(t => t.roles_Id == RoleId && t.ParentId != 0).ToList();
            return ParentList;
        }
        #endregion 
        #region 部门管理
        /// <summary>
        /// 部门列表显示及查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetSysDepartment(string name)
        {
            List<SysDepartment> list;
            if (name == null || name == "")
            {
                list = bll.GetList<SysDepartment>().ToList();
            }
            else
            {
                list = bll.GetList<SysDepartment>().Where(t => t.Department_Name.Contains(name)).ToList();
            }
            return JsonConvert.SerializeObject(list);
        }
        /// <summary>
        /// 部门添加
        /// </summary>
        /// <param name="dep"></param>
        /// <returns></returns>
        public bool AddSysDepartment(SysDepartment dep)
        {
            return bll.DAddEntity<SysDepartment>(dep);
        }
        /// <summary>
        /// 部门删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelSysDepartment(int id)
        {
            return bll.DDelEntity<SysDepartment>(id);
        }
        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="dep"></param>
        /// <returns></returns>
        public bool UpdSysDepartment(SysDepartment dep)
        {
            return bll.DUpdEntity<SysDepartment>(dep);
        }
        #endregion
        #region 角色管理
        /// <summary>
        /// 角色列表显示及查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetSysRoles(string name)
        {
            List<SysRoles> list;
            if (name == null || name == "")
            {
                list = bll.GetList<SysRoles>().ToList();
            }
            else
            {
                list = bll.GetList<SysRoles>().Where(t => t.roles_Name.Contains(name)).ToList();
            }
            return JsonConvert.SerializeObject(list);
        }
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool AddSysRoles(SysRoles role)
        {
            return bll.AddEntity<SysRoles>(role);
        }
        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        public bool DelSysRoles(int id)
        {
            return bll.DelEntity<SysRoles>(id);
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool UpdSysRoles(SysRoles role)
        {
            return bll.UpdEntity<SysRoles>(role);
        }
        #endregion
        #region 权限管理
        /// <summary>
        /// 权限显示列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetSysAuthority(string name)
        {
            List<SysAuthority> list;
            if (name == null || name == "")
            {
                list = bll.GetList<SysAuthority>().ToList();
            }
            else
            {
                list = bll.GetList<SysAuthority>().Where(t => t.authority_Name.Contains(name)).ToList();
            }
            return JsonConvert.SerializeObject(list);
        }
        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="id"></param>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        [HttpPost]
        public bool AddAuthorityRole(string aid, int RoleId)
        {
            if (aid.Length > 0)
            {
                bool bl = false;
                string result = aid.Substring(0, aid.Length - 1);
                string[] res = result.Split(',');

                var RoleList = bll.GetList<SysAuthorityAndRoles>().Where(t => t.roles_Id == RoleId).ToList();
                //如果通过角色查出角色权限关系表中存在数据则先删除在添加
                if (RoleList != null)
                {
                    foreach (var item in RoleList)
                    {
                        SysAuthorityAndRoles authrole = new SysAuthorityAndRoles();
                        authrole.authorityAndRoles_Id = item.authorityAndRoles_Id;
                        //bll.DelEntity<SysAuthorityAndRoles>(id);
                    }
                    foreach (var item in res)
                    {

                        var list = bll.GetList<SysAuthorityAndRoles>().Where(t => t.authority_Id == Convert.ToInt32(item)).FirstOrDefault();
                        SysAuthorityAndRoles authrole = new SysAuthorityAndRoles();
                        authrole.authority_Id = Convert.ToInt32(item);
                        authrole.authority_Name = list.authority_Name;
                        authrole.authority_Url = list.authority_Url;
                        authrole.ParentId = list.ParentId;
                        authrole.roles_Id = RoleId;
                        authrole.authorityAndRoles_CreatePeople = "";
                        bll.AddEntity<SysAuthorityAndRoles>(authrole);
                        bl = true;
                    }
                    if (bl == true)
                    {

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                ///直接分配权限
                else
                {
                    foreach (var item in res)
                    {

                        var list = bll.GetList<SysAuthorityAndRoles>().Where(t => t.authority_Id == Convert.ToInt32(item)).FirstOrDefault();
                        SysAuthorityAndRoles authrole = new SysAuthorityAndRoles();
                        authrole.authority_Id = Convert.ToInt32(item);
                        authrole.authority_Name = list.authority_Name;
                        authrole.authority_Url = list.authority_Url;
                        authrole.ParentId = list.ParentId;
                        authrole.roles_Id = RoleId;
                        authrole.authorityAndRoles_CreatePeople = "";
                        bll.AddEntity<SysAuthorityAndRoles>(authrole);
                        bl = true;
                    }
                    if (bl == true)
                    {

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="auth"></param>
        /// <returns></returns>
        public bool AddSysAuthority<T>(SysAuthority auth)
        {
            return bll.AddEntity<SysAuthority>(auth);
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelSysAuthority(int id)
        {
            return bll.DelEntity<SysAuthority>(id);
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="auth"></param>
        /// <returns></returns>
        public bool UpdSysAuthority(SysAuthority auth)
        {
            return bll.UpdEntity<SysAuthority>(auth);
        }
        #endregion
        #region 字典管理
        /// <summary>
        /// 显示或查询字典方法
        /// </summary>
        /// <param name="type">传回的类型</param>
        /// <returns></returns>
        //public string GetDictionary(string type)
        //{
        //    if (type == null)
        //    {

        //        return JsonConvert.SerializeObject(bll.GetList<SysDictionary>().Where(s => s.dictionary_ParentId != 0).ToList());
        //    }
        //    else
        //    {
        //        SqlParameter sp = new SqlParameter();
        //        sp.ParameterName = "dictionary_Type";
        //        sp.Value = type;
        //        return JsonConvert.SerializeObject(bll.GetList<SysDictionary>(sp).Where(s => s.dictionary_ParentId != 0).ToList());
        //    }
        //}
        /// <summary>
        /// 字典显示不带查询
        /// </summary>
        /// <returns></returns>
        public string GetDictionary()
        {
            return JsonConvert.SerializeObject(bll.GetList<SysDictionary>());
        }
        /// <summary>
        /// 添加字典
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public bool DicAddSysDictionary<T>(SysDictionary dic)
        {
            return bll.DicAddEntity<SysDictionary>(dic);
        }
        /// <summary>
        /// 删除字典
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public bool DicDelSysDictionary<T>(int id)
        {
            return bll.DicDelEntity<SysDictionary>(id);
        }
        /// <summary>
        /// 修改字典
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public bool DicUpdSysDictionary<T>(SysDictionary dic)
        {
            return bll.DicUpdEntity<SysDictionary>(dic);
        }
        #endregion
        #region 用户管理
        /// <summary>
        /// 用户列表显示及查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetUser(string name)
        {
            List<SysUser> list;
            if (name == null || name == "")
            {
                list = bll.GetList<SysUser>().ToList();
            }
            else
            {
                list = bll.GetList<SysUser>().Where(t => t.users_Name.Contains(name)).ToList();
            }
            return JsonConvert.SerializeObject(list);
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public bool UAddSysUser<T>(SysUser dic)
        {
            return bll.UAddEntity<SysUser>(dic);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public bool UDelSysUser<T>(int id)
        {
            return bll.UDelEntity<SysUser>(id);
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public bool UUpdSysUser<T>(SysUser dic)
        {
            return bll.UUpdEntity<SysUser>(dic);
        }
        #endregion
    }
}
